package com.wenhao.zhaoshang;

import java.util.Scanner;

/**
 * @author Wenhao Tong
 * @create 2021-08-08 18:59
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        for (int t  = 0;t < T;t++) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            long[][] A = new long[n][m];
            for (int i = 0;i < n;i++) {
                for (int j = 0;j < m;j++) {
                    A[i][j] = scanner.nextLong();
                }
            }
            boolean flag = true;
            lable:
            for (int i = 0;i < n - 1;i++) {
                for (int j = 0;j < m - 1;j++) {
                    if (A[i][j] != A[i + 1][j + 1]) {
                        flag = false;
                        break lable;
                    }
                }
            }
            if (flag) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}
